package com.example.hellomap;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyDrawerAdapter extends BaseAdapter{
	
	private Context context;
	private List<MyDrawerItem> mItemsList;
	
	public MyDrawerAdapter(Context context, List<MyDrawerItem> mItemsList){
		this.context = context;
		this.mItemsList = mItemsList;
	}

	@Override
	public int getCount() {
		return mItemsList == null ? 0 : mItemsList.size();
	}

	@Override
	public MyDrawerItem getItem(int position) {
		return mItemsList == null ? null : mItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = null;
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			row = LayoutInflater.from(context).inflate(R.layout.drawer_list_item, null);
			holder.mItemName = (TextView)row.findViewById(R.id.drawerItemName);
			row.setTag(holder);
		}else{
			row = convertView;
		}
		
		holder = (ViewHolder)row.getTag();
		MyDrawerItem menuItem = mItemsList.get(position);
		//Notice that SP conversion has been already performed during getDimensions
		holder.mItemName.setText(menuItem.mItemName);
		return row;
	}
	
	/**
	 * Return a list with the default elements to set
	 * on our DrawerLayout component
	 * @params context
	 */
	public static List<MyDrawerItem> getDefaultElementsList(Context context){
		List<MyDrawerItem> itemsList = new ArrayList<MyDrawerItem>();
		
		String[] itemsNames = context.getResources().getStringArray(R.array.drawer_array);
		for(int i = 0 ; i < itemsNames.length ; i++){
			MyDrawerItem item = new MyDrawerItem();
			item.mItemName = itemsNames[i];
			itemsList.add(item);
		}
		return itemsList;
	}
	
	/**
	 * DrawerMenuItem class is strictly linked to the use of this adapter, hence
	 * a Static Nested Class has been used for packaging convenience.
	 */
	public static class MyDrawerItem{
		private String mItemName;
		
		public String getItemName() {
			return mItemName;
		}
		public void setItemName(String itemName) {
			this.mItemName = itemName;
		}
	}
	
	/**
	 * ViewHolder linked to this Adapter
	 */
	private class ViewHolder{
		public TextView mItemName;
	}
}
